package com.shuqi.activity.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookInfo;

/**
 * 搜索主页gallery适配器
 * 
 * @author linyuren
 * 
 */
public class BookSearchImageAdapter extends BaseAdapter {

    private Activity context;
    private String gallerySecectId;// 当前选中的索引值
    /** 存储对象集合 */
    private List<BookInfo> bookSearchImageData;

    public BookSearchImageAdapter(Context context, List<BookInfo> data) {
        this.context = (Activity) context;
        this.bookSearchImageData = data;
    }

    public void setImageAdapter(List<BookInfo> data) {
        this.bookSearchImageData = data;
    }

    @Override
    public int getCount() {
        return (this.bookSearchImageData == null || this.bookSearchImageData.size() == 0) ? 0
                : Integer.MAX_VALUE;
    }

    @Override
    public BookInfo getItem(int position) {
        return this.bookSearchImageData == null ? null : this.bookSearchImageData.get(position
                % this.bookSearchImageData.size());
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setGallerySecectId(String index) {
        this.gallerySecectId = index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_act_booksearch_galleryimg, null);
            holder = new ViewHolder();
            holder.gallery_img = (ImageView) convertView.findViewById(R.id.gallery_img);
            holder.gallery_img_select =
                    (ImageView) convertView.findViewById(R.id.gallery_img_select);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BookInfo info = null;
        if (bookSearchImageData == null || bookSearchImageData.size() == 0
                || getItem(position) == null) {
            return convertView;
        } else {
            info = this.getItem(position);
        }
        final ViewHolder holder2 = holder;
        SqImageManager.getImageManager().displayImage(getItem(position).getBookCoverImgUrl(),
                holder2.gallery_img, SqImageManager.IMG_COVER);
        if (info != null) {
            if (this.gallerySecectId.equals(info.getBookId())) { // 设置是否显示选中框
                holder2.gallery_img_select.setVisibility(View.VISIBLE);
            } else {
                holder2.gallery_img_select.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    private static class ViewHolder {
        public ImageView gallery_img;
        public ImageView gallery_img_select;
    }

}
